package zj.com.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import zj.com.model.AuditLog;
import zj.com.model.DjcModel;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class DzgSpAdapter extends BaseAdapter{

    public List<AuditLog> list;
    public Context context;
    public LayoutInflater inflater;

    public DzgSpAdapter(List<AuditLog> list, Context context){

        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_dzg_sp_item, null);
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
            viewHolder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            viewHolder.tag_tv = (TextView) convertView.findViewById(R.id.tag_tv);
            viewHolder.reason_tv = (TextView) convertView.findViewById(R.id.reason_tv);
            viewHolder.date_tv = (TextView) convertView.findViewById(R.id.date_tv);
            viewHolder.head_iv = (SimpleDraweeView) convertView.findViewById(R.id.head_iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        AuditLog log = list.get(position);
        viewHolder.name_tv.setText(log.getAuditUser());
        viewHolder.tag_tv.setText(log.getAuditResult());
        if(TextUtils.isEmpty(log.getAuditDesc())){
            viewHolder.reason_tv.setText("(未填写原因)");

        }else{
            viewHolder.reason_tv.setText("("+log.getAuditDesc()+")");

        }
        viewHolder.date_tv.setText(log.getAuditDate().replace("T"," "));
        if(log.getAuditResult().equals("通过")){
            viewHolder.icon.setImageResource(R.drawable.sp_right_icon);

        }else{
            viewHolder.icon.setImageResource(R.drawable.sp_error_icon);

        }
        viewHolder.head_iv.setImageURI(Uri.parse("http://202.127.25.114:8081/api/sqcp/user/getAvatar?userUid="+log.getAuditUser()));
        return convertView;
    }
    private static class ViewHolder {


        ImageView icon;
        SimpleDraweeView head_iv;
        TextView name_tv;
        TextView tag_tv;
        TextView reason_tv;
        TextView date_tv;
    }

    public void setDataChange(List<AuditLog> list){

        this.list = list;
        notifyDataSetChanged();
    }
}
